package com.sophialex.health.record.lktable.join;

import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.commons.httpclient.util.DateUtil;

import com.sophia.dict.vo.DictCode;
import com.sophia.health.vo.DsGeography;
import com.sophia.health.vo.SystemUser;
import com.sophialex.health.common.platform.SuperContext;
import com.sophialex.health.record.common.showKtable.ISKJoinOutValue;

public class HrPersonQueryJoinOut implements ISKJoinOutValue {

	@Override
	public String getFieldValue(Object object, String fieldName, Object byValue) {

		if("sex".equals(fieldName)) {
			if(byValue != null) {
				try {
					Set<DictCode> set = SuperContext.dictCache.getCodesByName("SEX");
					Iterator it = set.iterator();
					while (it.hasNext()) {
						DictCode dict = (DictCode) it.next();
						if(dict.getCodeNum().equals(byValue.toString())) {
							return dict.getCodeValue();
						}
					}
				}
				catch(Exception e) {
					e.printStackTrace();
				}
			}
		}
		if("birthday".equals(fieldName)) {
			if(byValue != null && !"".equals(byValue)) {
				try {
					Date date = new Date(Date.parse(byValue.toString()));
					return DateUtil.formatDate(date, "yyyy-MM-dd");
				}
				catch(Exception e) {
					e.printStackTrace();
				}
			}
		}
		if("hamlet".equals(fieldName)){
			if(byValue != null && !"".equals(byValue)) {
				List<DsGeography> list = SuperContext.dsGeographyList;
				for(int i = 0; i < list.size(); i++) {
					DsGeography ds = list.get(i);
					if(ds.getId().longValue() == Long.valueOf(byValue.toString()).longValue()){
						return ds.getName();
					}
				}
			}
		}
		if("adminUserId".equals(fieldName) || "archiveUserId".equals(fieldName)){
			if(byValue != null && !"".equals(byValue)) {
				List<SystemUser> list = SuperContext.userList;
				for(int i = 0; i < list.size(); i++) {
					SystemUser ds = list.get(i);
					if(ds.getId().longValue() == Long.valueOf(byValue.toString()).longValue()){
						return ds.getUserName();
					}
				}
			}
		}
		return byValue == null ? "" : byValue.toString();
	}

}
